package cn.lixufeng.user.controller;

import cn.lixufeng.basic.exception.BusinessException;
import cn.lixufeng.basic.utils.AjaxResult;
import cn.lixufeng.basic.utils.PageList;
import cn.lixufeng.user.domain.User;
import cn.lixufeng.user.dto.UserDto;
import cn.lixufeng.user.query.UserQuery;
import cn.lixufeng.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 增加或修改
     * @param user
     * @return
     */
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId() == null) {
                userService.add(user);
            } else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public User findById(@PathVariable("id") Long id){
        return userService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<User> findAll(){
        return userService.findAll();
    }

    /**
     * 分页和高级查询
     * @param userQuery
     * @return
     */
    @PostMapping
    public PageList<User> findPage(@RequestBody UserQuery userQuery){
        return userService.findPage(userQuery);
    }

    /**
     * 批量删除
     * @param ids 是需要删除的id的集合，前台传的是数组，后台可以用数组或者集合来接收
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            userService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 手机注册
     * @param userDto
     * @returnh
     */
    @PostMapping("/phone/register")
    public AjaxResult register(@RequestBody UserDto userDto){// DTO -- 数据传输对象  前端输入的对象
        try {
            userService.register(userDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后再试！");
        }
    }
}
